﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssetManager.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the AssetManager class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.BusinessLogic
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using Lidocaine.BusinessObjects;
    using Lidocaine.Data;
    
    /// <summary>
    /// Defines the AssetManager class.
    /// </summary>
    public static class AssetManager
    {
        public static BusinessObjects.Asset New()
        {
            return new LazyAsset();
        }

        /// <summary>
        /// Initializes a new instance of the Asset class.
        /// </summary>
        /// <param name="fileId">The identity of the database table's row referencing this file.</param>
        public static BusinessObjects.Asset GetAsset(int fileId)
        {
            return Data.Asset.Select(fileId);
        }
        
        /// <summary>
        /// Finds a list of files with a given file type.
        /// </summary>
        /// <param name="fileType">The filetype of the files to search for.</param>
        /// <returns>A read-only list of files.</returns>
        public static List<BusinessObjects.Asset> GetFilesByFileType(BusinessObjects.FileType fileType)
        {
            return Data.Asset.SelectAll(fileType);
        }
        
        /// <summary>
        /// Finds a file based on its virtual path, if it exists.
        /// </summary>
        /// <param name="virtualPath">The virtual path to search for.</param>
        /// <returns>If the file is found, an Asset is populated and returned. Returns null otherwise.</returns>
        public static BusinessObjects.Asset GetFileByVirtualPath(string virtualPath)
        {
            return Data.Asset.Select(virtualPath);
        }

        public static List<Permission> GetPermissionsWithRoleInfo(string path)
        {
            return Data.Asset.GetPermissionsWithRoleInfo(path);
        }
        
        /// <summary>
        /// Inserts the Asset object into the database.
        /// </summary>
        /// <param name="asset">The asset to insert.</param>
        /// <returns>The inserted Asset with a newly populated Id field.</returns>
        public static BusinessObjects.Asset SaveAs(BusinessObjects.Asset asset)
        {
            return Data.Asset.Insert(asset);
        }

        /// <summary>
        /// Updates the Asset's database fields.
        /// </summary>
        /// <param name="asset">The asset to update.</param>
        public static void Save(BusinessObjects.Asset asset)
        {
            Data.Asset.Update(asset);
        }

        /// <summary>
        /// Deletes the Asset's database entry.
        /// </summary>
        /// <param name="asset">The asset to delete.</param>
        public static void Delete(BusinessObjects.Asset asset)
        {
            Data.Asset.Delete(asset);
        }
    }
}
